file = open("txt/test.txt", "r", encoding="utf-8")  # Open the file in read mode with UTF-8 encoding

result = file.read()

file.close()  # Close the file

print(result)
print("=================================================")

file = open("txt/test.txt", "r", encoding="utf-8")  # Open the file in read mode with UTF-8 encoding

result = file.readline()

file.close()  # Close the file

print(result)
print("=================================================")

file = open("txt/test.txt", "r", encoding="utf-8")  # Open the file in read mode with UTF-8 encoding

results = file.readlines()

file.close()  # Close the file

for result in results:
    print(result)

print("=================================================")

file = open("txt/test.txt", "r", encoding="utf-8")  # Open the file in read mode with UTF-8 encoding

for line in file:
    print(line, end="")

file.close()  # Close the file

print("")
print("=================================================")

"""
Using with statement to automatically close the file
通过with open语法打开文件，可以自动关闭文件，不需要手动调用close()方法
"""
with open("txt/test.txt", "r", encoding="utf-8") as file:  # Open the file in read mode with UTF-8 encoding

    for line in file:
        print(line, end="")

print("")
print("=================================================")

with open("txt/python.txt", "w", encoding="utf-8") as file:  # Open the file in read mode with UTF-8 encoding
    file.write("Hello, world!")

    file.flush()  # Write to the file immediately

print("File written successfully!")

with open("txt/python.txt", "r", encoding="utf-8") as file:  # Open the file in read mode with UTF-8 encoding
    for line in file:
        print(line, end="")

print("")
print("=================================================")